<?php
include("../user/userlogincheck.php");
include("../include/config.inc.php");

$sql="SELECT * FROM ".tableprefix."store where store_state='1' and member_id='$userid'";
$result=$db->query($sql);
$store=$db->fetch_array($result);
$db->free_result($result);

$sql2="SELECT * FROM ".tableprefix."store where store_state='0' and grade_id='0' and member_id='$userid'";
$res=$db->query($sql2);
$s=$db->fetch_array($res);
$db->free_result($res);

$sql3="SELECT * FROM ".tableprefix."store where store_state='0' and grade_id<>'0' and member_id='$userid'";
$res3=$db->query($sql3);
$s3=$db->fetch_array($res3);
$db->free_result($res3);

if($store["store_id"]<>''){
	echo "<script>location.href='../user/seller.php';</script>";
	exit;
}
if($s["store_id"]<>''){
	echo "<script>location.href='store_ok.php';</script>";
	exit;
}
if($s3["store_id"]<>''){
	echo "<script>location.href='store-grade.php';</script>";
	exit;
}
	
$todo=$_REQUEST["todo"];

if(isset($todo) && $todo=="todo")
{
	$sc_id='1';
	$store_flag=$_POST["store_flag"];
	$store_name=$_POST["store_name"];
	$province=$_POST["province"];
	$city=$_POST["city"];	
	$area=$_POST["area"];
	$store_address=$_POST["store_address"];	
	$seller_name=$_POST["seller_name"];
	$store_phone=$_POST["store_phone"];
	$grade_id=$_POST["grade_id"];	
	$time=time();
	$store_time=$time;
	$store_end_time=$time+86445;//3年
	$store_state='0';
	$area_info=$province.$city.$area;
	$store_company_name=$store_name;
	$store_pinyin='WYCN';
	$ip=getIP();
	$province_id='0';
	$city_id='0';
	$area_id='0';
	
	if($store_name==''){ 
		echo("<script type='text/javascript'>alert('请填写商家名称。');history.back(-1);</script>");
		exit;
	}
	if($area_info==''){ 
		echo("<script type='text/javascript'>alert('请选择地区。');history.back(-1);</script>");
		exit;
	}
	if($store_address==''){ 
		echo("<script type='text/javascript'>alert('请填写详细地址。');history.back(-1);</script>");
		exit;
	}
	if($seller_name==''){ 
		echo("<script type='text/javascript'>alert('请填写联系姓名。');history.back(-1);</script>");
		exit;
	}
	if($store_phone==''){ 
		echo("<script type='text/javascript'>alert('请填写联系电话。');history.back(-1);</script>");
		exit;
	}
	
	$sql="SELECT * FROM ".tableprefix."member where member_id='$userid'";
	$result=$db->query($sql);
	$m=$db->fetch_array($result);
	$member_name=$m["member_name"];
	$member_id=$m["member_id"];
	
	if(CountByKey(tableprefix."store","store_name",$store_name))
    {
		PhpBox("该名称已注册，请勿重复注册！");
		GotoPage("h");
		exit;  
	}	
		
	//$sql="insert into ".tablepre."store(store_name,store_pinyin,grade_id,member_id,member_name,seller_name,sc_id,store_company_name,province_id,city_id,area_id,area_info,store_address,store_state,store_time,store_end_time,store_phone,store_flag)values('$store_name','$store_pinyin','$grade_id','$member_id','$member_name','$seller_name','$sc_id','$store_company_name','$province_id','$city_id','$area_id','$area_info','$store_address','$store_state','$store_time','$store_end_time','$store_phone','$store_flag')";
	$sql="insert into ".tableprefix."store(store_name,store_pinyin,grade_id,member_id,member_name,seller_name,sc_id,store_company_name,province_id,city_id,area_id,area_info,store_address,store_state,store_time,store_end_time,store_phone,store_flag)values('$store_name','$store_pinyin','$grade_id','$member_id','$member_name','$seller_name','$sc_id','$store_company_name','$province_id','$city_id','$area_id','$area_info','$store_address','$store_state','$store_time','$store_end_time','$store_phone','$store_flag')";
	$db->query($sql);
	if(!$db->error())
	{
		if($grade_id>1){
			if($grade_id=='2'){
			$amount=1000.00;
			}elseif($grade_id=='3'){
			$amount=5000.00;
			}else{
			$amount=1000.00;
			}
			$sql="SELECT id FROM ".tableprefix."order order by id DESC limit 0,1;";
			$result=$db->query($sql);
			$s=$db->fetch_array($result);
			$orderid=$s["id"]+1000001;
			$orderNo='1'.date("ymdHis").$orderid;//业务编码+年的后2位+月+日+订单数(订单ID)[1 170515230101 1000001];
			$sql="insert into ".tableprefix."order(orderNo,userid,amount,ordername,payment,createtime,ip)values('$orderNo','$member_id','$amount','入驻保证金','wechatPay','$time','$ip')";
			$db->query($sql);
			if($grade_id=='2'){//推广期免费入驻
			$sql="update ".tableprefix."order set paystate='9',amount='0.00',orderinfo='推广期免费入驻',transaction_id='$orderNo',time_end='$time' where orderNo='$orderNo'";
			$db->query($sql);			
			$sql="update ".tableprefix."store set grade_id='$grade_id',store_state='1' where member_id='$userid'";
			$db->query($sql);
			GotoPage('paystatus.php?orderNo='.$orderNo);
			}else{
			GotoPage('../pay/wxpay/index.php?orderNo='.$orderNo);
			}
		}else{
			GotoPage('store_ok.php');
		}
	}
	else
	{
		PhpBox('数据库出错！');
		GotoPage('../index.php');		
	}
	$db->close();
	exit;
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>商家入驻 - 订戏会</title>
<meta name="renderer" content="webkit">
<meta http-equiv="X-UA-Compatible" content="IE=9,Chrome=1">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="apple-mobile-web-app-status-bar-style" content="black" />
<link rel="stylesheet" type="text/css" href="../css/frameCss/mui.picker.min.css"/>
<link rel="stylesheet" type="text/css" href="../css/frameCss/mui.poppicker.css"/>
<link rel="stylesheet" type="text/css" href="../css/public.css">
<link rel="stylesheet" type="text/css" href="../css/publish.css">
</head>

<body>
<div class="head-nav">
	<div class="head-left"><a href="#1"><div class="back">返回</div></a></div>
	<div class="head-center">商家入驻</div>
	<div class="head-right"><a href="#"><div class="shuoming"></div></a></div>
</div>
<div class="remind"><p>建议提交保证金，提高诚信度，获取更多用户信任。</p></div>
<form action="" method="post" name="publishForm" id="publishForm">
<input name="todo" type="hidden" value="todo">
<input type="hidden" name="province" value="">
<input type="hidden" name="city" value="">
<input type="hidden" name="area" value="">
<div class="mod-form">
	<div class="form-group">
		<div class="form-item">
			<label>商家性质</label>
			<span class="input-span">民营</span>
			<input name="store_flag" type="radio" value="2" class="input-radio">
			<span class="input-span">国有</span>
			<input name="store_flag" type="radio" class="input-radio" value="1" checked>
		</div>
		<div class="form-item">
			<label>商家名称</label>
			<input type="text" maxlength="60" id="store_name" name="store_name" placeholder="注册后不可修改，请认真填写" value="">
		</div>
		<div class="form-item">
			<label>所在地</label>
			<span id='showCityPicker3' style="left:80px;">点击选择地区</span>
			<div id='cityResult3' class="ui-alert"></div>
		</div>
		<div class="form-item">
			<label>详细地址</label>
			<input type="text" maxlength="60" id="store_address" name="store_address" placeholder="请填写商家详细地址" value="">
		</div>
		<div class="form-item">
			<label>联系姓名</label>
			<input type="text" maxlength="20" id="seller_name" name="seller_name" placeholder="请填写联系人姓名" value="">
		</div>
		<div class="form-item">
			<label>联系电话</label>
			<input type="text" maxlength="20" id="store_phone" name="store_phone" placeholder="请填写联系人电话" value="">
		</div>
		<div class="form-item">
			<label>保证金</label>
			<select name="grade_id">
  				<option value="1">普通商户</option>
  				<option value="2" selected>诚信商户</option>
				<option value="3">特约商户</option>
			</select>
		</div>
	</div>
	<div class="form-button"><input name="s" type="submit" id="s" value="提&nbsp;&nbsp;交"></div>
	<div class="form-readme"><p>我已阅读并同意<font color="#0099FF">《网演中国商家入驻协议》</font></p></div>
</div>
</form>

<footer id="bar-fixed">
	<a href="../" class="tab-item"><i class="icon-tabbar bar-home"></i>首页</a>
	<a href="../category.php" class="tab-item"><i class="icon-tabbar bar-message"></i>分类</a>
	<a href="../publish/" class="tab-item"><i class="icon-tabbar bar-publish"></i>发布</a>	
	<a href="../user/" class="tab-item active"><i class="icon-tabbar bar-user2"></i>我</a>
</footer>
<script src="../js/frameJS/mui.min.js" type="text/javascript" charset="utf-8"></script>
<script src="../js/frameJS/mui.picker.js" type="text/javascript" charset="utf-8"></script>
<script src="../js/frameJS/mui.poppicker.js" type="text/javascript" charset="utf-8"></script>
<script src="../js/frameJS/city.data-3.js" type="text/javascript" charset="utf-8"></script>
<script type="text/javascript">
	(function($, doc) {
		$.init();
		$.ready(function() {
			/**
			 * 获取对象属性的值
			 * 主要用于过滤三级联动中，可能出现的最低级的数据不存在的情况，实际开发中需要注意这一点；
			 * @param {Object} obj 对象
			 * @param {String} param 属性名
			 */
			var _getParam = function(obj, param) {
				return obj[param] || '';
			};
			
			//					//级联示例
			var cityPicker3 = new $.PopPicker({
				layer: 3
			});
			cityPicker3.setData(cityData3);
			var showCityPickerButton = doc.getElementById('showCityPicker3');
			var cityResult3 = doc.getElementById('cityResult3');
			document.publishForm.city.value=cityResult3;
			showCityPickerButton.addEventListener('tap', function(event) {
				cityPicker3.show(function(items) {
					cityResult3.innerText = "你选择的城市是:" + _getParam(items[0], 'text') + " " + _getParam(items[1], 'text') + " " + _getParam(items[2], 'text');
					document.publishForm.province.value=_getParam(items[0], 'text');
					document.publishForm.city.value=_getParam(items[1], 'text');
					document.publishForm.area.value=_getParam(items[2], 'text');					
					//返回 false 可以阻止选择框的关闭
					//return false;
					cityResult3.style.display = 'block';
					cityResult3.style.margin = '10px 0 0 15px';
					cityResult3.style.color = 'red';
				});
			}, false);
		});
	})(mui, document);
</script>
</body>
</html>